home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / mawk.zip / MAIN.C < prev    next >
C/C++ Source or Header  |  1991-04-22  |  3KB  |  152 lines

  1.  
  2. /********************************************
  3. main.c
  4. copyright 1991, Michael D. Brennan
  5.  
  6. This is a source file for mawk, an implementation of
  7. the Awk programming language as defined in
  8. Aho, Kernighan and Weinberger, The AWK Programming Language,
  9. Addison-Wesley, 1988.
  10.  
  11. See the accompaning file, LIMITATIONS, for restrictions
  12. regarding modification and redistribution of this
  13. program in source or binary form.
  14. ********************************************/
  15.  
  16.  
  17. /* $Log:    main.c,v $
  18.  * Revision 2.2  91/04/22  08:08:58  brennan
  19.  * cannot close(3) or close(4) because of bug in TurboC++ 1.0
  20.  * 
  21.  * Revision 2.1  91/04/08  08:23:27  brennan
  22.  * VERSION 0.97
  23.  * 
  24. */
  25.  
  26.  
  27.  
  28. /*  main.c  */
  29.  
  30. #include "mawk.h"
  31. #include "code.h"
  32. #include "init.h"
  33. #include "fin.h"
  34. #include "bi_vars.h"
  35. #include "field.h"
  36. #include "files.h"
  37. #include <stdio.h>
  38.  
  39. #if  DOS 
  40. void  reargv(int *, char ***) ;
  41. #endif
  42.  
  43.  
  44. void  PROTO( process, (void) ) ;
  45. void  PROTO( main_loop, (void) ) ;
  46.  
  47. extern int program_fd ;
  48. char *progname ;
  49.  
  50. jmp_buf   exit_jump, next_jump ;
  51. int  exit_code ;
  52.  
  53.  
  54. main(argc , argv )
  55.   int argc ; char **argv ;
  56.  
  57. #if   DOS
  58.   progname = "mawk" ;
  59. #if      HAVE_REARGV
  60.   reargv(&argc, &argv) ;
  61. #endif
  62. #else
  63.   { char *strrchr() ;
  64.     char *p = strrchr(argv[0], '/') ;
  65.     progname = p ? p+1 : argv[0] ; }
  66. #endif
  67.  
  68.   initialize(argc, argv) ;
  69.  
  70.   if ( parse() || compile_error_count )  exit(1) ;
  71.  
  72.   compile_cleanup() ;
  73.   process() ;
  74.  
  75.   mawk_exit( exit_code ) ;
  76.   return 0 ;
  77. }
  78.  
  79.  
  80. static  void  process()
  81. {
  82.  
  83.   if ( setjmp(exit_jump) )
  84.   { if ( begin_start ) zfree(begin_start, begin_size) ;
  85.     goto the_exit ;
  86.   }
  87.  
  88.   if ( begin_start )
  89.   { (void) execute(begin_start, eval_stack-1, 0) ;
  90.     zfree( begin_start , begin_size ) ;
  91.     begin_start = (INST *) 0 ;
  92.   }
  93.  
  94.   if ( main_start || end_start )  main_loop() ;
  95.  
  96. the_exit:
  97.  
  98.   if ( setjmp(exit_jump) )  mawk_exit(exit_code) ;
  99.  
  100.   if ( main_start )  zfree(main_start, main_size) ;
  101.   if ( end_start ) (void) execute(end_start, eval_stack-1, 0) ;
  102. }
  103.  
  104.  
  105. static void  main_loop()
  106. { register char *p ;
  107.   unsigned len ;
  108.  
  109.   /* the main file stream might already be open by a call of
  110.      getline in the BEGIN block */
  111.  
  112.   if ( main_fin == (FIN *) -1 && ! open_main()
  113.        || ! main_fin )  return ;
  114.  
  115.   if ( main_start )
  116.   {
  117.      (void)  setjmp(next_jump) ;
  118.  
  119.      while ( p = FINgets( main_fin, &len ) )
  120.      { 
  121.        if ( TEST2(bi_vars + NR) != TWO_DOUBLES )
  122.                     cast2_to_d(bi_vars + NR) ;
  123.  
  124.        bi_vars[NR].dval += 1.0 ;
  125.        bi_vars[FNR].dval += 1.0 ;
  126.  
  127.        set_field0(p, len) ;
  128.        (void) execute( main_start, eval_stack-1, 0) ;
  129.      }
  130.   }
  131.   else  /* eat main to set NR and FNR before executing END */
  132.   { long nr ;
  133.  
  134.     if ( TEST2(bi_vars+NR) != TWO_DOUBLES ) cast2_to_d(bi_vars+NR) ;
  135.     nr = (long) bi_vars[NR].dval ;
  136.     while ( FINgets( main_fin, &len ) )
  137.     { nr++ ; bi_vars[FNR].dval += 1.0 ; }
  138.     bi_vars[NR].dval = (double) nr ;
  139.   }
  140. }
  141.  
  142.  
  143. void  mawk_exit(x)
  144.   int x ;
  145. {
  146. #if  !  DOS
  147.   close_out_pipes() ;  /* no effect, if no out pipes */
  148. #endif
  149.   exit(x) ;
  150. }
  151.